/*
 * Copyright (c) 2006, Bob Breznak
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.iisageek.client;

import com.iisageek.client.widgets.xml.Node;
import com.iisageek.client.widgets.xml.Server;
import com.iisageek.client.widgets.xml.XMLHandler;

/**
 * Configure gets and parses "master_config.xml" to get the application's configuration values
 * and returns the values.  
 * 
 * There will be one instance in the lnl class of Configure in the application, otherwise
 * it is called as a static class.
 * 
 * @author Bob Breznak
 */

public class Configure implements XMLHandler {

	private static boolean loadError = false;
	private static Node config;

	/**
	 * Configure
	 * 
	 * This should only be called once in the application. 
	 */
	public Configure() {
		Server.requestData("https://users.wpi.edu/~lnl/databse2/master_config.xml", this);
	}

	/**
	 * receivedData
	 * 
	 * Invoked from the HTTP call in the constructor. Loads the configuration or sets internally
	 * a loading error
	 * 
	 * @param root (Node) the root XML node returned from the HTTP call  
	 */
	public void receivedData(Node root) {
		if(root.attributeExists("result")) {
			if(root.getAttribute("result").equals("configuration")) {
				Configure.config = root.getChild(0);
				return;
			}
		}

		loadError = true;
		config = null;
	}
	
	/**
	 * isError
	 * 
	 * returns if there was an error loading the configuration
	 * 
	 * @return boolean
	 */
	public static boolean isError() {
		return loadError;
	}
	
	/**
	 * getServer
	 * 
	 * returns the server to make HTTP request calls to
	 *  
	 * @return String
	 */
	public static String getServer() {
		return "https://users.wpi.edu/~lnl/database2";
	}
	
	/**
	 * getLowEventNumber
	 * 
	 * returns the number of events to be considered a "low" amount
	 * 
	 * @return int
	 */
	public static int getLowEventNumber() {
		if(config == null) return 0;
		return Integer.parseInt(config.getChild("low-event-number").getText());
	}
	
	/**
	 * getMedEventNumber
	 * 
	 * returns the number of events to be considerd a "medium" amount
	 * 
	 * @return int
	 */
	public static int getMedEventNumber() {
		if(config == null) return 4;
		return Integer.parseInt(config.getChild("med-event-number").getText());
	}
	
	/**
	 * getHighEventNumber
	 * 
	 * returns the number of events to be considered a "high" amount
	 * 
	 * @return int
	 */
	public static int getHighEventNumber() {
		if(config == null) return 6;
		return Integer.parseInt(config.getChild("high-event-number").getText());
	}
	
}
